use std::sync::Arc;

use actix_web::{App, HttpServer, Responder, web};
use serde::Deserialize;

use common::LResult;
use engine::DBRef;

use crate::state::State;
use crate::write::batch_write;

mod write;
mod state;

pub async fn start_http_service(db: DBRef) -> LResult<()> {
    let state = Arc::new(State::create(db));
    let service = HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(state.clone()))
            .service(batch_write)
    });
    service
        .bind(("127.0.0.1", 8080)).unwrap()
        .run().await.unwrap();
    Ok(())
}

#[cfg(test)]
mod test {
    // #[actix_web::test]
    // async fn main() -> LResult<()> {
    //     let pwd = current_dir().unwrap().into_os_string();
    //     println!("pwd: {}", pwd.to_str().unwrap());
    //     log4rs::init_file("../../conf/log4rs.yaml", Default::default()).unwrap();
    //     let storage = Arc::new(Storage::default());
    //     start_http_service(storage).await
    // }
}
